package com.ruoyi.business.controller;

import com.ruoyi.business.service.IWsdCompanyService;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.dto.CompanyDTO;
import com.ruoyi.common.core.domain.entity.WsdCompany;
import com.ruoyi.common.core.domain.vo.CompanyVO;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 公司概览信息Controller
 *
 * @author ruoyi
 * @date 2024-04-24
 */
@RestController
@RequestMapping("wsd_company")
@Slf4j
public class WsdCompanyController extends BaseController {

    @Autowired
    private IWsdCompanyService wsdCompanyService;

    /**
     * 查询公司概览信息
     */
//    @PreAuthorize("@ss.hasPermi('/wsd_company/list')")
    @GetMapping("/list")
    public AjaxResult list() {
        CompanyVO companyVO = wsdCompanyService.selectWsdCompany();
        return AjaxResult.success(companyVO);
    }


    /**
     * 新增公司概览信息
     */
    @PreAuthorize("@ss.hasPermi('business:company:add')")
    @Log(title = "公司概览信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CompanyDTO companyDTO) {
        return toAjax(wsdCompanyService.insertWsdCompany(companyDTO));
    }

    /**
     * 修改公司概览信息
     */
    @PreAuthorize("@ss.hasPermi('business:company:edit')")
    @Log(title = "公司概览信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CompanyDTO companyDTO) {
        return toAjax(wsdCompanyService.updateWsdCompany(companyDTO));
    }


    /**
     * 上传图片接口
     */
//    @PreAuthorize("@ss.hasPermi('business:company:upload')")
    @PostMapping(value = "/upload")
    @Anonymous
    public AjaxResult uploadFile(@RequestParam("file") MultipartFile file) {
        if (file.isEmpty()) {
            return error("请选择要上传的文件");
        }
        try {
            return new AjaxResult(200, "上传成功", wsdCompanyService.uploadFile(file));
        } catch (Exception e) {
            log.info( "上传图片错误信息============{}",e.getMessage() );
            //logger.info("上传图片错误信息---",e.getMessage());
            return error("上传失败");
        }
    }

    /**
     * 查询公司概览信息
     */
    @GetMapping("/applist")
    public AjaxResult applist() {
        CompanyVO companyVO = wsdCompanyService.selectWsdCompany();
        return AjaxResult.success(companyVO);
    }
}
